<%= form_for @ticket, url: tickets_url, html: { multipart: true } do |f| %>
  <% type = (!current_user.nil? && current_user.prefer_plain_text?) ? 'text' : 'html' %>

  <%= f.hidden_field :content_type, value: type %>

  <h3><%= t(:new_ticket) %></h3>

  <p>
    <%= f.text_field :from, tabindex: tabindex %>
  </p>

  <% if Tenant.current_tenant.ask_for_name %>
    <p>
      <%= f.text_field :name, tabindex: tabindex %>
    </p>
  <% end %>

  <% if !current_user.nil? && current_user.agent? %>
    <p>
      <% blank = @email_addresses.count == 0 ? EmailAddress.default_email : nil %>
      <%= f.label :to_email_address_id, t('activerecord.attributes.ticket.to'), class: 'ptm' %>
      <%= f.collection_select :to_email_address_id, @email_addresses, :id, :formatted,
        {include_blank: blank, label: false}, tabindex: tabindex %>
    </p>
  <% end %>

  <% if params[:label] %>
    <input name="label" type="hidden" value="<%= params[:label] %>"
  <% end %>

  <p>
    <%= f.text_field :subject, tabindex: tabindex %>
  </p>

  <% if can? :update, @ticket %>
    <p>
      <%
        options = [ [t(:unassigned), nil] ]
        @agents.ordered.map.each {|u| options << [u.email, u.id]}
      %>
    <%= f.select :assignee_id, options_for_select(options), {}, tabindex: tabindex %>
    </p>

    <p>
      <%
        options = Ticket.priorities.keys.map { |k| [t(k, scope: 'activerecord.attributes.ticket.priorities'), k] }
      %>
      <%= f.select :priority, options_for_select(options, f.object.priority), {}, tabindex: tabindex %>
    </p>
  <% end %>

  <p>
    <%
      content = ''
      unless current_user.nil?
        if current_user.prefer_plain_text?
          content = "\n#{html_to_text current_user.signature}"
        else
          content = "<p></p>#{current_user.signature}"
        end
      end
    %>

    <% if type == 'html' %>
      <%= f.trix_editor :content,
        value: (f.object.content.nil? ? content : f.object.content), tabindex: tabindex %>
    <% else %>
      <%= f.text_area :content,
        value: (f.object.content.nil? ? content : f.object.content), tabindex: tabindex %>
    <% end %>
  </p>

  <%= render 'attachments/form', f: f %>

  <% if Ticket.recaptcha_keys_present? && !user_signed_in? %>
    <p>
      <%= recaptcha_tags hl: I18n.locale %>
    </p>
  <% end %>

  <p>
    <%= f.submit t(:create_ticket), class: 'button regular radius' %>
  </p>

<% end %>
